package com.study.demo;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.input.Clipboard;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderStroke;
import javafx.scene.layout.BorderStrokeStyle;
import javafx.scene.layout.BorderWidths;
import javafx.scene.layout.CornerRadii;
import javafx.scene.paint.Paint;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

/**
*@description 实现窗口拖拽并且圆角设置
*@author xtayhame
*date 2020年3月8日
*version
*since v1.0
*/
public class DraggedWindowMain extends Application{
	double x=0;
	double y=0;

	public static void main(String[] args) {
		launch(args);
	}
	@Override
	public void start(Stage stage) throws Exception {
		AnchorPane anchorPane = new AnchorPane();
		//设置圆角
		Background background = new Background(new BackgroundFill(Paint.valueOf("#fff123"), new CornerRadii(15), new Insets(0)));
		anchorPane.setBackground(background);
		//设置边框
		//anchorPane.setBorder(new Border(new BorderStroke(Paint.valueOf("#987654"), BorderStrokeStyle.NONE, new CornerRadii(15),new BorderWidths(10) )));

		Scene scene = new Scene(anchorPane, 500, 400);
		//设置场景为白色，背景透明
		scene.setFill(Paint.valueOf("#ffffff00"));
		stage.setScene(scene);
		//设置为一个透明的窗体，没有最大化，最小化和关闭按钮
		stage.initStyle(StageStyle.TRANSPARENT);
		stage.show();

		scene.setOnMousePressed(new EventHandler<MouseEvent>() {

			@Override
			public void handle(MouseEvent event) {
				x=event.getScreenX()  - stage.getX();
				y=event.getScreenY() - stage.getY();
				System.out.println("x="+x + "    y="+y);
			}
		});

		scene.setOnMouseDragged(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				System.out.println("拖拽了");
				stage.setX(event.getScreenX() - x);
				stage.setY(event.getScreenY() - y);
			}
		});



	}
}
